package com.jhc.soft.service.impl;

import cn.hutool.json.JSONObject;
import com.jhc.soft.dto.LoginDto;
import com.jhc.soft.dto.ValidDto;
import com.jhc.soft.entity.User;
import com.jhc.soft.mapper.UserMapper;
import com.jhc.soft.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: zfm
 * @Date: 2021/11/11 09:13
 */
@Service
public class UserServiceImpl implements IUserService {

    private final static  String APP_ID="wx158f12baac0d5ab0";
    private final static  String SECRET="a4691eb78e2f5ebde0675ae578d093bb";

    @Autowired
    private UserMapper userMapper;

    @Override
    public User getById(int id) {
        return userMapper.getById(id);
    }

    /**
     * 登陆接口
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public Object login(String username, String password) {
        Map<String, Object> map = new HashMap<>();
        User user = userMapper.login(username, password);
        if (user == null) {
            // 用户名密码错误
            map.put("isOk", false);
        } else {
            map.put("isOk", true);
            map.put("info", user);
        }
        return map;
    }


@Override
    public  Object  zhuce(int id, String username, String password, String name)  {
        Map<String, Object> map = new HashMap<>();
        try {
            User panduan = userMapper.panduan(username);
            if (panduan == null) {
                User  user = userMapper.zhuce(id, username,password, name );
                map.put("isOk", true);
            } else {
                map.put("isOk", false);
            }
        }catch (Exception e){
            map.put("isOk", false);
        }
        return map;
    }


    //    校验用户是否绑定
    @Override
    public Object valid(ValidDto dto) {
        Map<String,Object> map = new HashMap<>();
        String code = dto.getCode();
        String URL = "https://api.weixin.qq.com/sns/jscode2session?appid="+APP_ID+"&secret="+SECRET+"&js_code="+code+"&grant_type=authorization_code";
        RestTemplate restTemplate = new RestTemplate();
        String obj = restTemplate.getForObject(URL,String.class);
        JSONObject jsonObject = new JSONObject(obj);
        String openId = jsonObject.getStr("openid");
        User user = userMapper.getByOpenId(openId);
        if (user == null){
//            未绑定
            map.put("isValid",false);
            map.put("openId", openId);
            return map;
        } else{
            map.put("isValid",true);
            map.put("info", user);
        }

        return map;
    }

    //    微信小程序登陆
@Override
    public Object loginByMin(LoginDto dto) {
        Map<String, Object> map = new HashMap<>();
        User user = userMapper.login(dto.getUsername(), dto.getPassword());
        if (user == null) {
            map.put("isOk",false);
            return map;
        }
        user.setOpenId(dto.getOpenId());

        userMapper.updateById(user);

        map.put("name", user.getName());
        map.put("isOk", true);
        return map;
    }

    @Override
    public Object zhuce(String username, String password, String name) {
        return null;
    }


}